package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.User;
import com.web.entity.ResponseResult;
import com.web.service.UserService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.annotation.Resource;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {

    @Resource
    private UserService userService;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //调用用户业务逻辑对象进行登录验证
        User user = userService.login(username, password);
        //转换为json，发送给前端
        String str =JSON.toJSONString(new ResponseResult(200,"OK",user));
        resp.getWriter().write(str);
    }
}
